// JavaScript Document

/*
*	addAdminAccount Form Validation
*	Author: Adam Fernandes
*	Created: June 22, 2008
*
*/


var errorMessage = "";

// new wrapper object
// var ap = new Object();

function addErrorMsg(message){
	errorMessage = errorMessage + message + "<br>";
}

function emailTest(email){
	var filter=/^([\w-]+(?:\.[\w-]+)*)@((?:[\w-]+\.)*\w[\w-]{0,66})\.([a-z]{2,6}(?:\.[a-z]{2})?)$/i
	if (filter.test(email)){
		return true;
	} else {
		return false;
	}
}

// simple validation for blank fields only
function validate(frm){

	var txtFlds = new Array();
	var errFlds = new Array();
	
	errorMessage = "<td colspan=\"2\"><strong>Errors:</strong><br>";
		
	for(var i = 0; i < frm.elements.length; i++){
		
		if(frm.elements[i].className=='req-err'){
			frm.elements[i].className = 'req';
		}
		
	}
	
	// grab all text fields
	for(var i = 0; i < frm.elements.length; i++){
		
		if(frm.elements[i].type=='text'){ //TEXT FIELDS

			if(frm.elements[i].name=='name'){
				if(frm.elements[i].value=='' || frm.elements[i].value==' ' || frm.elements[i].value == undefined){
					addErrorMsg("Please enter a valid Name");
					errFlds[errFlds.length] = frm.elements[i];
				}
			} else if(frm.elements[i].name=='email'){
				if (!emailTest(frm.elements[i].value)) {
					addErrorMsg("Please enter a valid Email Address");
					errFlds[errFlds.length] = frm.elements[i];
				}
			} else if(frm.elements[i].name=='username'){
				if(frm.elements[i].value=='' || frm.elements[i].value==' ' || frm.elements[i].value == undefined){
					addErrorMsg("Please enter a valid Username");
					errFlds[errFlds.length] = frm.elements[i];
				}
			} else if(frm.elements[i].name=='password'){
				if(frm.elements[i].value=='' || frm.elements[i].value==' ' || frm.elements[i].value == undefined){
					addErrorMsg("Please enter a valid Password");
					errFlds[errFlds.length] = frm.elements[i];
				}
			} else if(frm.elements[i].name=='password2'){
				if(frm.elements[i].value=='' || frm.elements[i].value==' ' || frm.elements[i].value == undefined){
					addErrorMsg("Please enter a valid Confirmation Password");
					errFlds[errFlds.length] = frm.elements[i];
				}
			} 
				
		} else if(frm.elements[i].type=='select-one'){ //SELECT
			if((frm.elements[i].selectedIndex==0) && (frm.elements[i].className=='req')){
				if(frm.elements[i].name=='birthMonth'){
					addErrorMsg("Please select a Birth Month");
					errFlds[errFlds.length] = frm.elements[i];
				}
			}
		} else if(frm.elements[i].type=='checkbox') { //CHECKBOX
			if(frm.elements[i].checked==false) {
				errFlds[errFlds.length] = frm.elements[i];
				addErrorMsg("Please check the Terms of Service check box to continue");
			}
		}
	
	}
	
	var pass1 = frm.password.value;
	var pass2 = frm.password2.value;
	
	if(pass1 != pass2){
		addErrorMsg("Your passwords do not match");
	} 
	
	if(errFlds.length>0 || pass1 != pass2){
		document.getElementById("errorMessages").innerHTML = errorMessage;
		parent.location='#error';
		return false;
	} else {
		//document.createAdmin.action = "addAdminAccount.jsp?success=true";
		return true;
	}
}


// utility functions

function isInteger(s)
{   var i;
    for (i = 0; i < s.length; i++)
    {   
        // Check that current character is number.
        var c = s.charAt(i);
        if (((c < "0") || (c > "9"))) return false;
    }
    // All characters are numbers.
    return true;
}


